/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.gerenciadorfinanceiro.ejb.session;

import java.util.List;
import org.gerenciadorfinanceiro.ejb.entity.Outcome;
import org.gerenciadorfinanceiro.ejb.entity.User;
import org.gerenciadorfinanceiro.ejb.session.interfaces.OutcomeSessionLocal;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.gerenciadorfinanceiro.ejb.session.interfaces.OutcomeSessionRemote;

/**
 *
 * @author eduardo
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class OutcomeSessionBean implements OutcomeSessionLocal, OutcomeSessionRemote {

    @PersistenceContext
    private EntityManager entityManager;

    public List<Outcome> getAllOutcomes(User u, int month, int year) {
        Query q = entityManager.createQuery("select o from Outcome o where o.owner = :owner and month(expirationData) = :mes and year(expirationData) = :ano");
        q.setParameter("owner", u);
        q.setParameter("mes", new Integer(month));
        q.setParameter("ano", new Integer(year));

        return q.getResultList();
    }

    public void addOutcome(Outcome o) throws BusinessViolation {
        this.validaOutcome(o);

        if (this.getOutcomeById(o.getId().intValue()) != null) {
            throw new BusinessViolation("Despesa já existente.");
        }

        entityManager.merge(o);
    }

    public void updateOutcome(Outcome o) throws BusinessViolation {
        this.validaOutcome(o);
        
        if (this.getOutcomeById(o.getId().intValue()) == null) {
            throw new BusinessViolation("Despesa inexistente.");
        }

        entityManager.merge(o);
    }

    public void deleteOutcome(Outcome o) throws BusinessViolation {
        if (this.getOutcomeById(o.getId().intValue()) == null) {
            throw new BusinessViolation("Despesa inexistente.");
        }

        Outcome aux = entityManager.getReference(Outcome.class, o.getId());
        entityManager.remove(aux);
    }

    public Outcome getOutcomeById(int id) {
        return entityManager.find(Outcome.class, new Integer(id));
    }
    
    public void validaOutcome(Outcome o) throws BusinessViolation {
        boolean valid = true;
        String msg = "";

        if (o.getOwner() == null) {
            valid = false;
            msg = "Owner inexistente.";
        }

        if (o.getSource() == null) {
            valid = false;
            msg = "Source inexistente.";
        }

        if (o.getValue() <= 0) {
            valid = false;
            msg = "Valor invalido.";
        }

        if (o.getDescriptionOutcome() == null || o.getDescriptionOutcome().isEmpty()) {
            valid = false;
            msg = "Descricao invalida.";
        }

        if (o.getData() == null) {
            valid = false;
            msg = "Data de cadastro invalida.";
        }

        if (o.getExpirationData() == null) {
            valid = false;
            msg = "Data da despesa invalida.";
        }

        if (! valid) {
            throw new BusinessViolation(msg);
        }
    }
 
}
